package com.wman.drawing;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import java.nio.channels.AsynchronousChannelGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


    private TextView mTextView;

    Handler mUiHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
                case 0:
                    Log.e("Handler", "UI线程 handleMessage");
                    mTextView.setText(mTextView.getText() + " -> UI线程 handleMessage");
                    break;
                case 1:
                    Log.e("Handler", "子线程 handleMessage");
                    mTextView.setText(mTextView.getText() + " -> 子线程 handleMessage");
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTextView = findViewById(R.id.textview);
        Log.e("Handler", "当前线程ID: " + Thread.currentThread().getId());
        //持有UI线程的handler对象进行消息发送到UI线程
        new Thread() {
            @Override
            public void run() {
                super.run();
                Log.e("Handler", "线程ID: " + Thread.currentThread().getId());
//                Looper.prepare();
                Handler handler = new Handler(Looper.getMainLooper()) {
                    @Override
                    public void handleMessage(@NonNull Message msg) {
                        super.handleMessage(msg);
                        mTextView.setText("子线程 handleMessage");
                        Log.e("Handler", "当前线程ID: " + Thread.currentThread().getId());
                    }
                };
                handler.sendEmptyMessage(1);
//                Looper.loop();
            }
        }.start();
    }

    class test extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] objects) {
            return null;
        }
    }
}
